package com.taptech.doufu.presenter.home;

import android.app.Activity;
import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.util.CacheUtil;
import com.taptech.doufu.bean.novel.VipAlbumBean;
import com.taptech.doufu.bean.novel.VipMixWorksBean;
import com.taptech.doufu.bean.novel.VipTopicBean;
import com.taptech.doufu.listener.CommmonImp;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Wyatt on 2017/10/13/013.
 */

public class VipTopicPresenter {
    public interface OnHomeTopicView{
        void onTopicNext(List<VipTopicBean> list, boolean isCache);
        void onTopicError();
    }

    private OnHomeTopicView onHomeTopicView;
    private Context mContext;

    public VipTopicPresenter(Context context,OnHomeTopicView onHomeTopicView) {
        this.mContext = context;
        this.onHomeTopicView = onHomeTopicView;
    }

    /**
     * 获取缓存
     */
    public void loadVipTopicCache() {
        Observable.create(new Observable.OnSubscribe<List<VipTopicBean>>() {
                    @Override
                    public void call(Subscriber<? super List<VipTopicBean>> subscriber) {
                        org.json.JSONObject json = CacheUtil.readJsonDataFromCache(new File(CommmonImp.Home_vip_list));
                        List<VipTopicBean> list = formatData(json.toString());
                        subscriber.onNext(list);
                    }
                })
                .compose(RxJavaHelper.<List<VipTopicBean>>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<List<VipTopicBean>>(){
                    @Override
                    public void onNext(List<VipTopicBean> list) {
                        super.onNext(list);
                        if (onHomeTopicView != null && list!=null) {
                            onHomeTopicView.onTopicNext(list,true);
                        }
                    }
                });
    }

    /**
     * 获取话题数据
     */
    public void getVipTopic(final boolean isUseCache) {

        Call<ResponseBody> call =  ApiClient.getInstance().setUseCache(isUseCache).getService().getVipTopic();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                try {
                    String json = new String(response.body().bytes());
                    CacheUtil.saveJsonDataToCache(json,new File(CommmonImp.Home_vip_list));
                    onHomeTopicView.onTopicNext(formatData(json),isUseCache);
                } catch (Exception e) {
                    onHomeTopicView.onTopicError();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                onHomeTopicView.onTopicError();
            }
        });
    }

    private List<VipTopicBean> formatData(String json) {
        List<VipTopicBean> items = new ArrayList<>();
        JSONObject jsonObject = JSON.parseObject(json);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray list = data.getJSONArray("list");
        for (int i = 0; i < list.size(); i++) {
            VipTopicBean bean = new VipTopicBean();
            JSONObject object = list.getJSONObject(i);
            if (object.containsKey("title")) {
                String title = object.getString("title");
                bean.setTitle(title);
            }
            if (object.containsKey("model")) {
                int model = object.getInteger("model");
                bean.setModel(model);
            }
            if (object.containsKey("more")) {
                int more = object.getInteger("more");
                bean.setMore(more);

            }
            if (object.containsKey("url")) {
                String url = object.getString("url");
                bean.setUrl(url);
            }
            if (object.containsKey("moreUrl")) {
                String moreUrl = object.getString("moreUrl");
                bean.setMoreUrl(moreUrl);
            }
            if (object.containsKey("weexUrl")) {
                bean.setWeexUrl(object.getString("weexUrl"));
            }
            if (object.containsKey("moreTitle")) {
                bean.setMoreTitle(object.getString("moreTitle"));
            }
            if (object.containsKey("list")) {
                String array = object.getString("list");
                List<VipMixWorksBean> list1 = JSON.parseArray(array,VipMixWorksBean.class);
                List<VipAlbumBean> list2 = JSON.parseArray(array, VipAlbumBean.class);
                bean.setSimpleList(list1);
                bean.setList(list2);
            }
            items.add(bean);
        }
        return items;
    }

}
